`
https://leetcode.cn/problems/word-pattern/
`

/**
 * @param {string} pattern
 * @param {string} s
 * @return {boolean}
 */
var wordPattern = function (pattern, s) {
  const strs = s.split(' ')
  // 长度不对应，则肯定不会匹配
  if (pattern.length !== strs.length) {
    return false
  }

  // 存放字母到单词的映射
  const patternToWord = new Map()
  // 存放已经被映射的单词
  const words = new Set()
  for (let i = 0; i < pattern.length; i++) {
    const c = pattern[i], word = strs[i]
    // 存放新的映射
    if (!patternToWord.has(c)) {
      // 该单词是否已经被映射
      if (words.has(word)) {
        return false
      }
      patternToWord.set(c, word)
      words.add(word)
      continue
    }

    // 已有映射，判断是否与之前映射的单词相同
    if (patternToWord.get(c) !== word) {
      return false
    }
  }

  return true
};